<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="margin-t">
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input v-model="queryInfo.query" placeholder="请输入内容" @clear="searchKey" clearable>
            <el-button slot="append" type="primary" icon="el-icon-search" @click="searchKey"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品 -->
        <el-col :span="2">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodsList" style="width: 100%" class="margin-t" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">{{scope.row.upd_time|formatTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods(scope.row)"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete-solid"
              size="mini"
              @click="removeGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <el-dialog title="修改商品" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="修改名称">
          <el-input v-model="editForm.name" disabled></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      queryInfo: {
        //请求参数
        query: "", //查询参数
        pagenum: 1, //当前页码
        pagesize: 10 //每页显示条数
      },
      goodsList: [], //商品列表数据
      total: 0, //商品列表总条数,
      editDialogVisible: false, //修改对话框展示隐藏
      editForm: {
        //修改对话框表单数据
        name: ""
      }
    };
  },
  methods: {
    async getGoodsData() {
      //请求商品表格数据
      const { data: res } = await this.$axios.get("goods", {
        params: this.queryInfo
      });
      if (res.meta.status === 200) {
        this.goodsList = res.data.goods;
        this.total = res.data.total;
      }
    },
    searchKey() {
      //搜索
      this.getGoodsData();
    },
    handleSizeChange(newSize) {
      //显示条数改变触发事件
      this.queryInfo.pagesize = newSize;
      this.getGoodsData();
    },
    handleCurrentChange(newPage) {
      //页码变化触发事件
      this.queryInfo.pagenum = newPage;
      this.getGoodsData();
    },
    goAddPage() {
      //添加商品跳转页面
      this.$router.push("goods/add");
    },
    async removeGoods(row) {
      //删除商品
      const confirm = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirm === "confirm") {
        const { data: res } = await this.$axios.delete(`goods/${row.goods_id}`);
        if (res.meta.status === 200) {
          this.$message.success("删除成功");
        }
        this.getGoodsData();
      } else {
        this.$message.error("已取消删除");
      }
    },
    editGoods(row) {
      //修改商品
      this.editDialogVisible = true;
      console.log(row);
      this.editForm.name = row.goods_name;
    }
  },
  created() {
    this.getGoodsData();
  }
};
</script>

<style  scoped>
.margin-t {
  margin-top: 20px;
}
</style>